package com.jed.xxh.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jed.xxh.R;
import com.jed.xxh.utils.L;
import com.jed.xxh.utils.StaticClass;
import com.jed.xxh.utils.T;
import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 项目名：SmartButler<br/>
 * 包名：  com.jed.xxh.ui<br/>
 * 作者：徐晓辉<br/>
 * 创建日期：2018/11/27 14:46<br/>
 * 描述：手机号码归属地<br/>
 */
public class PhoneActivity extends BaseActivity implements View.OnClickListener {

    private EditText tv_phone;
    private ImageView iv_company;
    private TextView tv_result;
    private Button btn_query_phone_number;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone);
        initView();
    }

    private void initView() {
        tv_phone = findViewById(R.id.tv_phone_number);
        tv_result = findViewById(R.id.tv_result);
        iv_company = findViewById(R.id.iv_company);
        btn_query_phone_number = findViewById(R.id.btn_query_phone_number);
        btn_query_phone_number.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_query_phone_number:
                getPhoneBelonging();
                break;
        }
    }

    /**
     * 查询手机号码归属地
     */
    private void getPhoneBelonging() {
        String phoneNumber = tv_phone.getText().toString().trim();
        if (!TextUtils.isEmpty(phoneNumber)) {
            HttpParams httpParams = new HttpParams();
            httpParams.put("key", StaticClass.JH_PHONE_KEY);
            httpParams.put("phone", phoneNumber);
            RxVolley.get(StaticClass.JH_PHONE_URL, httpParams, new HttpCallback() {
                @Override
                public void onSuccess(String t) {
                    L.i("查询手机号码归属地的结果为：" + t);
                    paresJson(t);
                }
            });
        } else {
            T.showShortToast(this.getString(R.string.text_tost_empty));
        }
    }

    /**
     * 解析json并设置页面中的值
     */
    private void paresJson(String t) {
        try {
            JSONObject jsonObject = new JSONObject(t);
            String error_code = jsonObject.getString("error_code");
            int resultCode = Integer.parseInt(error_code);
            switch (resultCode) {
                case 0:
                    JSONObject result = jsonObject.getJSONObject("result");
                    String company = result.getString("company");
                    tv_result.setText("归属地：" + result.getString("province")
                            + result.getString("city") + "\n"
                            + "区号：" + result.getString("areacode") + "\n"
                            + "邮编：" + result.getString("zip") + "\n"
                            + "类型：" + company);
                    switch (company) {
                        case "移动":
                            iv_company.setBackgroundResource(R.drawable.china_mobile);
                            break;
                        case "联通":
                            iv_company.setBackgroundResource(R.drawable.china_unicom);
                            break;
                        case "电信":
                            iv_company.setBackgroundResource(R.drawable.china_telecom);
                            break;
                    }
                    break;
                case 201101:
                    T.showShortToast("手机号码不能为空");
                    break;
                case 201102:
                    T.showShortToast("错误的手机号码");
                    break;
                case 201103:
                default:
                    T.showShortToast("查询无结果");
                    break;
            }
        } catch (JSONException e) {
            T.showShortToast(PhoneActivity.this.getString(R.string.request_error));

        }
    }
}
